//
//  regularlyRemindVC.m
//  CMAccountBook
//
//  Created by 王开政 on 2018/10/31.
//  Copyright © 2018年 王开政. All rights reserved.
//

#import "regularlyRemindVC.h"

@interface regularlyRemindVC ()<UITableViewDelegate,UITableViewDataSource>{
    
    pulicMath *math;
}
@property(nonatomic, strong) UITableView *Tab;

@property(nonatomic, strong) NSMutableArray *muArray;

@property(nonatomic, strong) UIButton *addRemindButton;
@end

@implementation regularlyRemindVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    math = [[pulicMath alloc]init];
    
    [self navig];
    self.muArray = [[NSMutableArray alloc]init];
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSData *remindArray_data = [math readLocalCacheDataWithKey:@"remindArray_data"];
    if (remindArray_data != nil) {
        NSArray *array =[NSKeyedUnarchiver unarchiveObjectWithData:remindArray_data];
        [self.muArray addObjectsFromArray:array];
        
    }
    [self.view addSubview:self.Tab];
    [self.view addSubview:self.addRemindButton];
//    [self fireLocalMessage];
    // Do any additional setup after loading the view.
}
#pragma mark-自定义导航区
-(void)navig{
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBarHidden=YES;
    __weak typeof(self) weakself = self;
    HomeNavigation*navView = [[HomeNavigation alloc]initWithTag:1 title:@"定时提醒" handler:^(UIButton *sender) {
        [weakself backPress];
    }];
    [self.view addSubview:navView];
    
}
-(void)backPress{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
-(UIButton *)addRemindButton{
    if (!_addRemindButton) {
        _addRemindButton = [[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight -SafeAreaTabbarHeight, ScreenWidth, SafeAreaTabbarHeight)];
        [_addRemindButton setTitle:@"添加提醒" forState:0];
        [_addRemindButton setTitleColor:[UIColor colorWithHexString:@"#101010"] forState:0];
        _addRemindButton.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        [_addRemindButton addTarget:self action:@selector(addRemindPress) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addRemindButton;
}
-(UITableView *)Tab{
    
    if (!_Tab) {
        _Tab = [[UITableView alloc]initWithFrame:CGRectMake(0,SafeAreaTopHeight, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
        _Tab.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _Tab.delegate = self;
        _Tab.dataSource = self;
        //        self.myTab.scrollEnabled = NO;
        _Tab.showsVerticalScrollIndicator = NO;
        _Tab.tableFooterView.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        if(@available(iOS 11.0, *)){
            _Tab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _Tab.estimatedRowHeight = 0;
            _Tab.estimatedSectionHeaderHeight = 0;
            _Tab.estimatedSectionFooterHeight = 0;
            
        }
        
    }
    
    return _Tab;
}
#pragma mark -设置cell横线左顶边
-(void)viewDidLayoutSubviews {
    if ([self.Tab respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.Tab setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.Tab respondsToSelector:@selector(setLayoutMargins:)])  {
        [self.Tab setLayoutMargins:UIEdgeInsetsZero];
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}
#pragma  make - UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
   
    return self.muArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        
    }
//    cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text =@"定时提醒";
    cell.detailTextLabel.text = self.muArray[indexPath.row][@"title"];
    return cell;
}

#pragma  make - UITableViewDelegate
//返回高度，计算不同高度cell 一定要用，否则会出现偏移
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return 50;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    view.backgroundColor = [UIColor colorWithHexString:@"#F6F6F8"];
    //    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(21*kfit, 0, ScreenWidth/2, 35)];
    //    leftLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
    //    leftLabel.font = [UIFont systemFontOfSize:10*kfit];
    //    leftLabel.textAlignment = NSTextAlignmentLeft;
    //    leftLabel.text =self.sectionArray[section][@"data"];
    //
    //    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2 -21*kfit, 0, ScreenWidth/2, 35)];
    //    rightLabel.textColor = [UIColor colorWithHexString:@"#B7B7B7"];
    //    rightLabel.font = [UIFont systemFontOfSize:10*kfit];
    //    rightLabel.textAlignment = NSTextAlignmentRight;
    //    rightLabel.text =self.sectionArray[section][@"money"];
    //
    //    [view addSubview:leftLabel];
    //    [view addSubview:rightLabel];
    
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
   
    
}
-(void)addRemindPress{
    
    [self fireLocalMessage];
}
// 发出本地通知
- (void)fireLocalMessage {
    
    /*
     ios8以上版本需要在appdelegate中注册申请权限 本地通知在软件杀死状态也可以接收到消息
     */
    
    // 1.创建本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    // 2.设置本地通知(发送的时间和内容是必须设置的)
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
    localNote.alertBody = @"吃饭了么?";
    
    /**
     其他属性: timeZone 时区
     repeatInterval 多长时间重复一次:一年,一个世纪,一天..
     region 区域 : 传入中心点和半径就可以设置一个区域 (如果进入这个区域或者出来这个区域就发出一个通知)
     regionTriggersOnce  BOOL 默认为YES, 如果进入这个区域或者出来这个区域 只会发出 一次 通知,以后就不发送了
     alertAction: 设置锁屏状态下本地通知下面的 滑动来 ...字样  默认为滑动来查看
     hasAction: alertAction的属性是否生效
     alertLaunchImage: 点击通知进入app的过程中显示图片,随便写,如果设置了(不管设置的是什么),都会加载app默认的启动图
     alertTitle: 以前项目名称所在的位置的文字: 不设置显示项目名称, 在通知内容上方
     soundName: 有通知时的音效 UILocalNotificationDefaultSoundName默认声音
     可以更改这个声音: 只要将音效导入到工程中,localNote.soundName = @"nihao.waw"
     */
    
    localNote.alertAction = @"提醒"; // 锁屏状态下显示: 滑动来快点啊
    //    localNote.alertLaunchImage = @"123";
    localNote.alertTitle = @"每天 15:30";
    localNote.soundName = UILocalNotificationDefaultSoundName;
    localNote.soundName = @"sound.wav";
    
    /* 这里接到本地通知,badge变为5, 如果打开app,消除掉badge, 则在appdelegate中实现
     [application setApplicationIconBadgeNumber:0];
     */
    localNote.applicationIconBadgeNumber = 5;
    
    // 设置额外信息,appdelegate中收到通知,可以根据不同的通知的额外信息确定跳转到不同的界面
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",self.muArray.count],@"type",localNote.alertTitle,@"title",nil];
    localNote.userInfo =dic;
    // 3.调用通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
    [self.muArray addObject:dic];
    NSLog(@"%@",localNote.userInfo);
    NSLog(@"%@",dic);
    NSLog(@"%@",self.muArray);
    NSData *remindArray_data = [NSKeyedArchiver archivedDataWithRootObject:self.muArray];
    [math writeLocalCacheData:remindArray_data withKey:@"remindArray_data"];
    NSLog(@"%ld",self.muArray.count);
    [self.Tab reloadData];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
